

Vue.createApp({
    // 定义组件
    components:{
        'left-nav': LEFT_NAV,
        "user-info": USER_INFO,
    },
    // 初始化
    setup(){
        // reactive定义变量运行存储
        // toRefs将数据返回
        const {
            reactive, toRefs
        } = Vue
        // 定义变量，即所需数据
        const state = reactive({
            user: {},
            blocks: [],
            menu: [
                {
                    name: "基础",
                    icon: 'list ul icon'
                },
                {
                    name: "我的",
                    icon: 'address card icon'
                },
                {
                    name: "拉黑",
                    icon: 'ban icon'
                },
                {
                    name: "赞赏",
                    icon: 'thumbs up icon'
                },
                {
                    name: "账号",
                    icon: 'user circle icon'
                },
            ]
        })
        $.get("/loginUser", function(data){
            state.user = data
        });
        $.get("/getBlock", function(data){
            state.blocks = data
            console.log(data);
        });
        exchangeDisplay("#setting #nav li", "#setting #detail>div")
        // 返回数据
        return {
            ...toRefs(state)
        }
    },
    methods:{
        deleteAccount() {
            swalConfirm("删除账号", "账号将永久删除", 'error', 
            function() {
                swalSuccess("删除账号成功", "账号将永久删除")
                get("/deleteAccount")
                jump("/login", 1500, "跳转主页中...")
            }, swalError("操作已取消", "账户信息依旧保留"))
        },
        update(index) {
            // 更新账户
            if (index == 0) {
                let receive = $("#setting input[name='receive']").val()
                let remind = $("#setting input[name='remind']").val()
                post("/updateAccount/basic", {
                    // name, password, address,tel, email));
                    name: this.user.name,
                    password: this.user.password,
                    address: this.user.address,
                    tel: this.user.tel,
                    email: this.user.email,
                    receive: receive,
                    remind: remind
                }, function() {
                    swalSuccess("更新成功", "账户信息已更新")
                })
            } else if (index == 1) {
                post("/updateAccount/me", {
                    profile: this.user.profile
                }, function() {
                    swalSuccess("更新成功", "个人信息信息已更新")
                })
            } else if (index == 2) {
                let rewardProfile = $("textarea[name='rewardProfile']").val().trim()
                console.log(rewardProfile);
                if (rewardProfile == "") {
                    swalError("更新失败", "打赏描述信息不能为空")
                    return
                }
                post("/updateReward", {
                    rewardProfile: rewardProfile
                }, function() {
                    swalSuccess("更新成功", "打赏信息已更新")
                })
            }
        },
        removeBlock(index, user) {
            console.log("移除");
            let that = this
            get("removeBlock/" + user.userId)
            that.blocks.splice(index, 1);
            swalSuccess("移除", "已将" + user.name + "从黑名单中移除")
          
        }
    }
    // 绑定app
}).mount("#setting")